package com.newjiabo.applet.controller;

import com.newjiabo.applet.modules.entity.dto.PlaceOrderDTO;
import com.newjiabo.common.ski.media.module.entity.dto.WeChatPayDto;
import com.newjiabo.applet.modules.service.CashierService;
import com.newjiabo.common.core.domain.AjaxResult;
import com.newjiabo.ski.domain.dto.SkiAfterSaleDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 买家端,收银台接口
 *
 * @author CaoMengHui
 */
@RestController
@RequestMapping("/applet/payment/cashier")
@Api(tags = "小程序-收银台接口")
public class CashierController {
    @Resource
    private CashierService cashierService;
    private static final Logger log = LoggerFactory.getLogger(CashierService.class);

    /**
     * // TODO <CaoMengHui>: 1、下单之后不允许修改订单金额。2、不支持代付款
     * @param orderDTO
     * @return
     */
    @ApiOperation(value = "下单")
    @PostMapping(value = "/placeOrder")
    public AjaxResult placeOrder(@RequestBody @Validated PlaceOrderDTO orderDTO) {
        return AjaxResult.success(cashierService.placeOrder(orderDTO));
    }


    @PostMapping(value = "/pullUpPayment")
    @ApiOperation(value = "返回前段拉起支付所需要的参数")
    public AjaxResult pullUpPayment(@RequestBody @Validated WeChatPayDto weChatPayDto, HttpServletRequest request){
        return AjaxResult.success(cashierService.pullUpPayment(weChatPayDto,request));
    }

    /**
     * 回调接口
     *
     * @param request
     * @param response
     */
    @RequestMapping("/payCallback")
    public void payCallback(HttpServletRequest request, HttpServletResponse response) {
        log.info("=============进入微信支付回调接口================");
        cashierService.payCallback(request,response);
    }

    @PostMapping(value = "/applyService")
    @ApiOperation(value = "申请售后服务")
    public AjaxResult applyService(@RequestBody SkiAfterSaleDTO dto){
        return AjaxResult.success(cashierService.applyService(dto));
    }

    @RequestMapping("/refundWeChatNotify")
    public void refundWeChatNotify(HttpServletRequest request,HttpServletResponse response){
        log.info("=============进入微信退款回调接口================");
        cashierService.refundWeChatNotify(request,response);
    }
}
